package com.suanfa.dp;

/**
 * 打家劫舍， 抢相邻的客户会被发现先报警，最后一家和第一家相邻
 */
public class Thief2 {

    public static void main(String[] args) {
        int[] arr = {1,2,3};
        System.out.println(rob(arr));
    }

    public static int rob(int[] nums){
        if(nums.length == 1){
            return  nums[0];
        }
        if(nums.length == 2){
            return Math.max(nums[0], nums[1]);
        }

        return Math.max(getMax(nums, 1, nums.length-1), getMax(nums, 0, nums.length-2));

    }



    public  static int getMax(int[] arr, int start, int end){
        int max = 0;
        int pre = 0;
        int cur = 0;

        for(int i = start; i<= end; i++){
            max = Math.max(cur, pre + arr[i]);
            pre = cur;
            cur = max;
        }
        return max;

    }
}
